package com.intech.android.demo.cms.decorators;


import com.intech.android.demo.cms.bo.TextContent;
import com.intech.android.demo.cms.bo.Group;
import com.intech.android.demo.cms.bo.Region;
import com.intech.android.demo.cms.common.Constants;
import com.intech.android.demo.cms.enums.Status;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class TextContentDecorator
{
    private static final Logger log = Logger.getLogger(TextContentDecorator.class);

    private String adID;
    private String category;
    private String name;
    private String advertiser;
    private String description;
    private List<String> viewingGroups;
    private List<String> regions;
    private String budget;
    private String payPerView;
    private String status;
    private String delayTime;


    public TextContentDecorator(TextContent textContent)
    {
        adID = textContent.getId().toString();
        category = textContent.getCategory().getName();
        name = textContent.getName();
        advertiser = textContent.getAdvertiser();
        description = textContent.getDescription();
        this.delayTime = textContent.getDelayTime().toString();
        viewingGroups = new ArrayList<String>();
        regions = new ArrayList<String>();
        for (Group group : textContent.getGroups())
        {
            viewingGroups.add(group.getName());
        }

        for (Region region : textContent.getRegions())
        {
            regions.add(region.getName());
        }
        payPerView = textContent.getPayment() == null ? "" : textContent.getPayment().toString();

        Double contentBudget = textContent.getBudget();
        if (contentBudget == null)
        {
            budget = Constants.Settings.EMPTY_STRING;
            status = Status.Inactive.getMessage();
        }
        else
        {
            budget = contentBudget.toString();
            if (contentBudget < Constants.Logic.MIN_BUDGET)
            {
                status = Status.Inactive.getMessage();
            }
            else
            {
                status = Status.Active.getMessage();
            }
        }

    }

    public String getDelayTime()
    {
        return delayTime;
    }

    public void setDelayTime(String delayTime)
    {
        this.delayTime = delayTime;
    }

    private boolean solveStatus(String status)
    {
        if (status == "active")
        {
            return true;
        }
        return false;
    }


    public String getAdvertiser()
    {
        return advertiser;
    }

    public void setAdvertiser(String advertiser)
    {
        this.advertiser = advertiser;
    }

    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }

    public String getPayPerView()
    {
        return payPerView;
    }

    public void setPayPerView(String payPerView)
    {
        this.payPerView = payPerView;
    }

    public String getBudget()
    {

        return budget;
    }

    public void setBudget(String budget)
    {
        this.budget = budget;
    }

    public String getRegions()
    {
        String regionStr = "";
        for (String region : regions)
        {
            regionStr += region + ",";
        }
        return regionStr;
    }


    public String getViewingGroups()
    {
        String viewingGroupsStr = "";
        for (int i = 0; i < this.viewingGroups.size(); i++)
        {
            viewingGroupsStr += this.viewingGroups.get(i);
            if (i < this.viewingGroups.size() - 1)
            {
                viewingGroupsStr += ",";
            }
        }
        return viewingGroupsStr;
    }

    public void setViewingGroups(String viewingGroupsStr)
    {
        viewingGroups = new ArrayList<String>();
        String tempString = "";
        for (int i = 0; i < viewingGroupsStr.length(); i++)
        {
            if (viewingGroupsStr.charAt(i) != ',')
            {
                tempString += viewingGroupsStr.charAt(i);
            }
            else
            {
                viewingGroups.add(tempString);
                tempString = "";
            }
        }
    }

    public String getDescription()
    {

        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public String getName()
    {

        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getCategory()
    {

        return category;
    }

    public void setCategory(String category)
    {
        this.category = category;
    }

    public String getAdID()
    {

        return adID;
    }

    public void setAdID(String adID)
    {
        this.adID = adID;
    }

    public String getRemaining()
    {
        Double remaining = Double.parseDouble(budget) / 2;
        return remaining.toString();
    }

    public void setRemaining(String remaining)
    {
    }
}
